<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="加油站名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入加油站名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['shop:petrol:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['shop:petrol:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['shop:petrol:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['shop:petrol:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="petrolList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="45" align="center" />
      <el-table-column label="主键" align="center" prop="id" width="50"/>
      <el-table-column label="加油站名" align="center" prop="name" width="135"/>
      <el-table-column label="联系方式" align="center" prop="phone" width="120" />
      <el-table-column label="每周营业时间" align="center" prop="week" width="100" />
      <el-table-column
        label="开始营业时间"
        align="center"
        prop="startTime"
        width="100"
      >
        <!-- <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>  -->
      </el-table-column>
      <el-table-column
        label="结束营业时间"
        align="center"
        prop="endTime"
        width="100"
      >
        <!-- <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template> -->
      </el-table-column>
      <el-table-column label="汽油类型" align="center" prop="chePetrolOil" width="80">
        <template slot-scope="scope">
          <div v-if="scope.row.chePetrolOil != null">
            <div v-for="(item, index) in scope.row.chePetrolOil" :key="index">
              <dict-tag :options="dict.type.oil_type" :value="item.oilId" style="margin:5px 0"/>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="详细位置" align="center" prop="address" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="100"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['shop:petrol:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['shop:petrol:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改加油站对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="加油站名" prop="name">
          <el-input v-model="form.name" placeholder="请输入加油站名" />
        </el-form-item>
        <el-form-item label="联系方式" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系方式" />
        </el-form-item>
        <el-form-item label="每周营业时间" prop="week">
          <el-input v-model="form.week" placeholder="请输入每周营业时间" />
        </el-form-item>
        <el-form-item label="开始营业时间" prop="startTime">
          <!-- <el-date-picker clearable
            v-model="form.startTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择开始营业时间">
          </el-date-picker> -->
          <el-time-select
            v-model="form.startTime"
            :picker-options="{
              start: '08:00',
              step: '00:30',
              end: '21:00',
            }"
            placeholder="选择时间"
          >
          </el-time-select>
        </el-form-item>
        <el-form-item label="结束营业时间" prop="endTime">
          <!-- <el-date-picker
            clearable
            v-model="form.endTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择结束营业时间"
          >
          </el-date-picker> -->
          <el-time-select
            v-model="form.endTime"
            :picker-options="{
              start: '08:00',
              step: '00:30',
              end: '21:00',
            }"
            placeholder="选择时间"
          >
          </el-time-select>
        </el-form-item>
        <el-form-item label="详细位置">
          <el-cascader
            placeholder="请选择省市区"
            v-model="address1"
            :options="citys"
          ></el-cascader>
        </el-form-item>
        <el-form-item prop="address">
          <el-input v-model="form.address" placeholder="请输入详细位置" />
        </el-form-item>
        <!-- <el-form-item label="经度" prop="longitude">
          <el-input v-model="form.longitude" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="维度" prop="latitude">
          <el-input v-model="form.latitude" placeholder="请输入维度" />
        </el-form-item> -->
        <el-form-item label="加油站坐标">
          <el-button @click="getLocation">获取位置</el-button>
          <span v-show="form.latitude && form.longitude">
            {{ form.latitude }},{{ form.longitude }}
          </span>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listPetrol,
  jdwd,
  getPetrol,
  delPetrol,
  addPetrol,
  updatePetrol,
} from "@/api/shop/petrol";

import citys from "@/utils/citydata.js";
import axios from "axios";

export default {
  name: "Petrol",
  dicts: ['oil_type'],
  data() {
    return {
      //省市区数组
      address1: [],
      //省市区工具
      citys: citys,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 加油站表格数据
      petrolList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      jdwdList: {
        key: "457886b5972a97e93fb9cdccebfd450f",
        address: "",
      },
    };
  },
  created() {
    this.getList();
  },
  watch: {
    address1() {
      this.form.province = this.address1[0];
      this.form.city = this.address1[1];
      this.form.district = this.address1[2];
    },
  },
  methods: {
    /**获取经纬度 */
    getLocation() {
      (this.jdwdList.address =
        this.form.province +
        this.form.city +
        this.form.district +
        this.form.address),
        axios
          .get(
            "https://restapi.amap.com/v3/geocode/geo?key=457886b5972a97e93fb9cdccebfd450f&address=" +
              this.jdwdList.address
          )
          .then((resp) => {
            // console.log(resp);
            let location = resp.data.geocodes[0].location;
            console.log(location)
            this.form.longitude = location.split(",")[0];
            this.form.latitude = location.split(",")[1];
            console.log(this.form.longitude, this.form.latitude);
          });
    },
    /** 查询加油站列表 */
    getList() {
      this.loading = true;
      listPetrol(this.queryParams).then((response) => {
        this.petrolList = response.rows;
        console.log(response.rows);
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        phone: null,
        week: null,
        startTime: null,
        endTime: null,
        address: null,
        longitude: null,
        latitude: null,
        province: null,
        city: null,
        district: null,
      };
      this.address1 = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加加油站";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getPetrol(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改加油站";
        this.address1 = [
          this.form.province,
          this.form.city,
          this.form.district,
        ];
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updatePetrol(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPetrol(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除加油站编号为"' + ids + '"的数据项？')
        .then(function () {
          return delPetrol(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "shop/petrol/export",
        {
          ...this.queryParams,
        },
        `petrol_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
